package com.zhuoshi.action;

//import javax.servlet.http.HttpServletRequest;

//import org.apache.struts2.ServletActionContext;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhuoshi.bean.Wd_userModel;
import com.zhuoshi.service.Wd_userModelService;

@Controller
@RequestMapping("/login1")
public class Wd_userAction {
	@Autowired
	private Wd_userModelService userModelService;

	@RequestMapping("lg.do ")
	public String lg() {
		return "login";
	}

	@RequestMapping("login.do ")
	public String login(HttpServletRequest request, Wd_userModel bean,
			Model model) {
		System.out.println(bean.getName() + "----------");
		Wd_userModel user = userModelService.selectUser(bean);
		if (user == null) {
			String name = bean.getName();
			String password = bean.getPassword();
			/*
			 * if(name==null && password==null){ return "login"; }else{
			 */
			model.addAttribute("mes", "错误的用户名密码");
			return "login";
			// }
		} else {
			// model.addAttribute("user",user);
			request.getSession().setAttribute("user", user);
			return "index";
		}
	}
	
	//查詢全部用戶
	@RequestMapping("selectuser.do")
	public String selectUser(Model model){
		List<Wd_userModel> list = userModelService.selectAllUser();
		model.addAttribute("userlist", list);
		return "Wd_User_select";
	}
	
	//跳到添加用户页面
	@RequestMapping("user_add.do")
	public String toAddUser(){
		return "Wd_User_add";
	}
	
	//添加
	@RequestMapping("add_user.do")
	public String addUser(Wd_userModel userBean){
		userModelService.insertSelective(userBean);
		return "redirect:selectuser.do";
	}
	
	//删除
	@RequestMapping("delete_user.do")
	public String deleteUser(String userId){
		System.out.println(userId+"iiiiiiiiiiiiiiiii");
		userModelService.deleteByPrimaryKey(userId);
		return "redirect:selectuser.do";
	}
	
	//修改获取值
	@RequestMapping("toupdate_user.do")
	public String toupdate(Wd_userModel uBean,Model model){
		Wd_userModel ubean = userModelService.selectByPrimaryKey(uBean.getUserId());
		model.addAttribute("ubean", ubean);
		return "Wd_User_update";
	}
	
	//修改
	@RequestMapping("update_user.do")
	public String update(Wd_userModel userBean){
		userModelService.updateByPrimaryKeySelective(userBean);
		return "redirect:selectuser.do";
	}
}
